package vn.gq.tym.model.item;

import org.json.JSONException;
import org.json.JSONObject;
import vn.gq.tym.comictemplate.Configuation;
import android.content.Context;
import android.content.SharedPreferences;

public class AdmobItem {
	
	public static final String TYPE_BANNER = "banner";
	public static final String TYPE_FULLSCREEN = "fullscreen";
	public static final String KEY_LOADED = "loaded";
	
	
	private String banner = Configuation.DEFAULT_ADID;
	private String fullscreen = Configuation.DEFAULT_ADID;
	private boolean loaded = false;
	
	public AdmobItem() {
		
	}
	
	public AdmobItem(String banner, String fullscreen) {
		setAdsTypeBanner(banner);
		setAdsTypeFullScreen(fullscreen);
	}
	
	public AdmobItem(JSONObject input) {
		try {
			setAdsTypeBanner(input.getString(TYPE_BANNER));
			setAdsTypeFullScreen(input.getString(TYPE_FULLSCREEN));
			setLoaded(input.getBoolean(KEY_LOADED));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public void setAdsTypeBanner(String _banner) {
		banner = _banner;
	}
	
	public void setAdsTypeFullScreen(String _fullscreen) {
		fullscreen = _fullscreen;
	}
	
	public String getAdsTypeBanner() {
		return banner;
	}
	
	public String getAdsTypeFullScreen() {
		return fullscreen;
	}
	
	public JSONObject toJSON() {
		JSONObject saveObj = new JSONObject();
		try {
			saveObj.put(TYPE_BANNER, banner);
			saveObj.put(TYPE_FULLSCREEN, fullscreen);
			saveObj.put(KEY_LOADED, loaded);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return saveObj;
	}
	
	public void setPreference(Context context) {
		SharedPreferences pref = context.getSharedPreferences("pref_ads", Context.MODE_PRIVATE);
		pref.edit().putString("adUnitId", toJSON().toString()).commit();
	}
	
	public static AdmobItem load(Context context) {
		SharedPreferences pref = context.getSharedPreferences("pref_ads", Context.MODE_PRIVATE);
		String admobItemStr = pref.getString("adUnitId", new AdmobItem().toJSON().toString());
		if (admobItemStr != null) {
			try {
				AdmobItem item = new AdmobItem(new JSONObject(admobItemStr));
				return item;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return new AdmobItem();
	}

	public boolean isLoaded() {
		return loaded;
	}

	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
}
